package com.cmcc.pp.ui.platformmonitoring;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cmcc.pp.R;
import com.cmcc.pp.base.BFragment;
import com.cmcc.pp.ui.MianActivityDrawerLayout;
import com.cmcc.pp.ui.widget.RefreshLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by ABC on 2017/12/21.
 * 平台监控
 */

public class PlatformMonitoringFragment extends BFragment {

    @Bind(R.id.platformmonitoring_tv_nomalynetworkelement)
    TextView tvNormalElement;

    @Bind(R.id.content)
    RefreshLinearLayout refreshLinearLayout;
    @Bind(R.id.platformmonitoring_butt_left)
    RadioButton platformmonitoringButtLeft;
    @Bind(R.id.platformmonitoring_butt_right)
    RadioButton platformmonitoringButtRight;
    @Bind(R.id.platformmonitoring_radiogroup)
    RadioGroup platformmonitoringRadiogroup;
    @Bind(R.id.item_relat)
    RelativeLayout itemRelat;

    private Handler handler;
    private boolean flag;

    @Override
    protected boolean getHasTitle() {
        return true;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_platformmonitoring;
    }

    @SuppressLint("HandlerLeak")
    @Override
    public void initView(View fragmentView) {
        getHeader().setTitle(R.string.str_platformmoitoring);
        getHeader().showLeftImage(R.mipmap.iocn_menubar);

        platformmonitoringRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId) {
                    case R.id.platformmonitoring_butt_left://刷新
                        //点击执行逻辑
                        Message message = Message.obtain();
                        message.what = 3;
                        handler.sendMessage(message);
                        flag = false;
                        break;

                    case R.id.platformmonitoring_butt_right://业务拨测

                        Intent intent = new Intent(getActivity(),ResultForServiceDialingActivity.class);
                        startActivity(intent);


                    default:
                        break;
                }
            }
        });


        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getFAIL());
                        platformmonitoringButtLeft.setChecked(false);
                        break;
                    case 2:
                        refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getSUCCESS());
                        break;
                    case 3:
                        if (flag) {
                            refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getLOADING());
                            handler.sendEmptyMessageDelayed(2, 5000);
                        } else {
                            refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getLOADING());
                            handler.sendEmptyMessageDelayed(1, 5000);
                        }
                        break;
                }
            }
        };

    }

    @Override
    public void loadData() {

    }


    @OnClick({
            R.id.item_relat
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_relat://跳转到所选择网元的服务器列表
                openServerlist();
                break;
        }
    }

    //服务器列表
    private void openServerlist() {
        Intent serverIntent = new Intent(getActivity(),ServerlistActivity.class );
        startActivity(serverIntent);
    }

    @Override
    protected void onLeftClick() {
        openmeunbar();
    }

    //打开菜单
    private void openmeunbar() {
        MianActivityDrawerLayout mianActivityDrawerLayout = (MianActivityDrawerLayout) getActivity();
        mianActivityDrawerLayout.closemenu();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
